<template>
	<el-dialog :title="form.id ? '编辑' : '新增'" v-model="visible" :close-on-click-modal="false" draggable width="600">
		<el-form ref="dataFormRef" :model="form" :rules="dataRules" formDialogRef label-width="90px" v-loading="loading">
			<el-row :gutter="24">
				<el-col :span="24" class="mb20">
					<el-form-item label="设备类型" prop="deviceTypeId">
						<el-select v-model="form.deviceTypeId" clearable filterable placeholder="请选择设备类型" @change="getModelList">
							<el-option v-for="item in deviceTypeList" :key="item.id" :value="item.id" :label="item.typeName" :disabled="item.state === 20" />
						</el-select>
					</el-form-item>
				</el-col>

				<el-col :span="24" class="mb20">
					<el-form-item label="设备型号" prop="deviceModelId">
						<el-select v-model="form.deviceModelId" clearable filterable placeholder="请选择设备型号">
							<el-option
								v-for="item in deviceModelList"
								:key="item.id"
								:value="item.id"
								:label="item.deviceModelName"
								:disabled="item.state === 20"
							/>
						</el-select>
					</el-form-item>
				</el-col>
				<el-col :span="24" class="mb20">
					<el-form-item label="报警类型" prop="alarmTypeId">
						<el-select v-model="form.alarmTypeId" clearable filterable placeholder="请选择报警类型">
							<el-option v-for="item in alarmTypeList" :key="item.id" :value="item.id" :label="item.typeName" :disabled="item.state === 10" />
						</el-select>
					</el-form-item>
				</el-col>
				<el-col :span="24" class="mb20">
					<el-form-item label="事件名称" prop="eventTypeName">
						<el-input v-model="form.eventTypeName" placeholder="请输入事件名称" show-word-limit maxlength="50" />
					</el-form-item>
				</el-col>
				<el-col :span="24" class="mb20">
					<el-form-item label="类型ID" prop="eventTypeCode">
						<el-input v-model="form.eventTypeCode" placeholder="请输入类型ID" show-word-limit maxlength="50" />
					</el-form-item>
				</el-col>
				<el-col :span="24" class="mb20" v-if="!form.id">
					<el-form-item label="状态" prop="state">
						<el-radio-group v-model="form.state">
							<el-radio :label="20">启用</el-radio>
							<el-radio :label="10">禁用</el-radio>
						</el-radio-group>
					</el-form-item>
				</el-col>

				<el-col :span="24" class="mb20">
					<el-form-item label="备注" prop="remarks">
						<el-input type="textarea" resize="none" show-word-limit :rows="5" v-model="form.remarks" placeholder="请输入备注" maxlength="100" />
					</el-form-item>
				</el-col>
			</el-row>
		</el-form>
		<template #footer>
			<span class="dialog-footer">
				<el-button @click="visible = false">取消</el-button>
				<el-button type="primary" @click="onSubmit" :disabled="loading">确认</el-button>
			</span>
		</template>
	</el-dialog>
</template>

<script setup lang="ts" name="AlarmEventTypeDialog">
import { useDict } from '/@/hooks/dict';
import { useMessage } from '/@/hooks/message';
import { getObj, addObj, putObj } from '/@/api/alarm/alarmEventType';
import { fetchListAll } from '/@/api/platform/basicDeviceType';
import { fetchListAll as fetchAllModelList } from '/@/api/platform/basicDeviceModel';
import { fetchListAll as fetchAllAlarmTypeList } from '/@/api/alarm/alarmType';
import { rule } from '/@/utils/validate';
const emit = defineEmits(['refresh']);

// 定义变量内容
const dataFormRef = ref();
const visible = ref(false);
const loading = ref(false);
const deviceTypeList: any = ref([]);
const deviceModelList: any = ref([]);
const alarmTypeList: any = ref([]);
// 定义字典

// 提交表单数据
const form = reactive({
	id: '',
	eventTypeCode: '',
	eventTypeName: '',
	remarks: '',
	state: 20,
	deviceTypeId: '',
	deviceTypeName: '',
	deviceModelId: '',
	deviceModelName: '',
	alarmTypeId: '',
	alarmTypeName: '',
	alarmTypeCode: '',
});

// 定义校验规则
const dataRules = ref({
	eventTypeName: [
		{ required: true, message: '事件名称不能为空', trigger: 'blur' },
		{
			validator: rule.checkSpaceInline,
			trigger: 'blur',
		},
	],
	eventTypeCode: [
		{ required: true, message: '类型ID不能为空', trigger: 'blur' },
		{
			validator: rule.checkSpaceInline,
			trigger: 'blur',
		},
	],
	deviceTypeId: [{ required: true, message: '设备类型不能为空', trigger: 'change' }],
	deviceModelId: [{ required: true, message: '设备型号不能为空', trigger: 'change' }],
	alarmTypeId: [{ required: true, message: '报警类型不能为空', trigger: 'change' }],
});

// 打开弹窗
const openDialog = (id: string) => {
	visible.value = true;
	form.id = '';

	// 重置表单数据
	nextTick(() => {
		dataFormRef.value?.resetFields();
	});

	// 获取alarmEventType信息
	if (id) {
		form.id = id;
		getalarmEventTypeData(id);
	}
	getDeviceTypes();
	getAlarmTypeList();
};

// 提交
const onSubmit = async () => {
	const valid = await dataFormRef.value.validate().catch(() => {});
	if (!valid) return false;

	try {
		loading.value = true;
		const deviceModelItem = deviceModelList.value.find((d) => d.id === form.deviceModelId);
		form.deviceModelName = deviceModelItem.deviceModelName;
		const deviceTypeItem = deviceTypeList.value.find((d) => d.id === form.deviceTypeId);
		form.deviceTypeName = deviceTypeItem.typeName;
		const alarmTypeItem = alarmTypeList.value.find((d) => d.id === form.alarmTypeId);
		form.alarmTypeName = alarmTypeItem.typeName;
		form.alarmTypeCode = alarmTypeItem.typeCode;
		form.id ? await putObj(form) : await addObj(form);
		useMessage().success(form.id ? '修改成功' : '添加成功');
		visible.value = false;
		emit('refresh');
	} catch (err: any) {
		useMessage().error(err.msg);
	} finally {
		loading.value = false;
	}
};

// 初始化表单数据
const getalarmEventTypeData = (id: string) => {
	// 获取数据
	loading.value = true;
	getObj(id)
		.then((res: any) => {
			getModelList(res.data.deviceTypeId);
			Object.assign(form, res.data);
		})
		.finally(() => {
			loading.value = false;
		});
};
// 获取设备数据
const getDeviceTypes = () => {
	fetchListAll({ state: 10 }).then((res) => {
		deviceTypeList.value = res.data;
	});
};
const getAlarmTypeList = () => {
	fetchAllAlarmTypeList({ state: 20 }).then((res) => {
		alarmTypeList.value = res.data;
	});
};
// 查询设备型号
const getModelList = (deviceTypeId: string) => {
	form.deviceModelId && (form.deviceModelId = null);
	fetchAllModelList({ deviceTypeId, state: 10 }).then((res) => {
		deviceModelList.value = res.data;
	});
};
// 暴露变量
defineExpose({
	openDialog,
});
</script>
